﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace UrlChecker.Tools
{
    public partial class ImportUrl : GlassForm
    {
        private static int type = 0;

        public ImportUrl()
        {
            InitializeComponent();
        }

        public ImportUrl(int i)
        {
            InitializeComponent();
            type = i;
        }

        private void ImportUrl_Load(object sender, EventArgs e)
        {
            DataTable dt = QT.SEO.Group.GetAll();
            this.cbbGroup.ValueMember = dt.Columns[0].ToString();
            this.cbbGroup.DisplayMember = dt.Columns[1].ToString();
            this.cbbGroup.DataSource = dt;
            this.cbbGroup.SelectedIndex = 0;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cbbGroup.SelectedItem == null)
            {
                MessageBox.Show("请选择分组");
                return;
            }

            int Success = 0, Failed = 0, Skip = 0;

            this.pbImport.Minimum = 0;
            this.pbImport.Value = 0;
            this.pbImport.Visible = true;
            if (type == 0)
            {
                this.pbImport.Maximum = ((URL)this.Owner).lvResult.Items.Count;

                foreach (ListViewItem item in ((URL)this.Owner).lvResult.Items)
                {
                    switch (Import(item))
                    {
                        case 1:
                            Success++;
                            break;
                        case -2:
                            Skip++;
                            break;
                        default:
                            Failed++;
                            break;
                    }

                    this.pbImport.Value++;
                }
            }
            else
            {
                this.pbImport.Maximum = ((URL)this.Owner).lvResult.SelectedItems.Count;

                foreach (ListViewItem item in ((URL)this.Owner).lvResult.SelectedItems)
                {
                    switch (Import(item))
                    {
                        case 1:
                            Success++;
                            break;
                        case -2:
                            Skip++;
                            break;
                        default:
                            Failed++;
                            break;
                    }

                    this.pbImport.Value++;
                }
            }

            MessageBox.Show("导入完成。成功：" + Success + " 失败：" + Failed + " 跳过：" + Skip);

            this.Close();
        }

        private int Import(ListViewItem item)
        {
            int rVal = -1;
            string guid = Guid.NewGuid().ToString().Replace("-", "");

            if ((rVal = QT.SEO.URL.Add(guid, item.SubItems[1].Text, this.cbbGroup.SelectedValue.ToString())) <= 0) return rVal;

            rVal = QT.SEO.SnapShot.Add(guid, item.SubItems[3].Text, item.SubItems[7].Text, item.SubItems[2].Text, item.SubItems[5].Text);

            return rVal;
        }
    }
}
